//
//  SYMyFmdb.m
//  BSHOUYIFANG
//
//  Created by 曹雁彬 on 15/8/22.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYMyFmdb.h"

@implementation SYMyFmdb
-(void)createTableUserInfo
{
          NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *filrPath = [str stringByAppendingPathComponent:@"like.db"];
    NSLog(@"%@",filrPath);
         _db = [FMDatabase databaseWithPath:filrPath];
        if (![_db open]) {
            NSLog(@"%@",filrPath);
           
        }
    if (![self isExistTable:@"userInfo"]) {
         [_db executeUpdate:@"create table userInfo (userName text, phoneNumber text, password text,icon BLOB)"];
        SYUser *user = [[SYUser alloc]init];
        user.phoneNumber = @"8888";
        user.password = @"8888";
        user.username  = @"admin";
        UIImage *image = [UIImage imageNamed:@"circle_default_face"];
        user.icon = UIImagePNGRepresentation(image);
        [self addWithUser:user];
    }
    
    
        
    
}
//通过单例 创建一个表
+ (SYMyFmdb *)shareInstance
{
        static SYMyFmdb *manager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            manager = [[SYMyFmdb alloc]init];
            [manager createTableUserInfo];
        });
        return manager;
 }
/**
 *  更新数据库用户信息
*/
- (void)updataWithUser:(SYUser *)user
{
    if ([self findUserInfophoneNumber:user.phoneNumber]) {
        [_db executeUpdate:@"UPDATE userInfo SET icon = ? WHERE phoneNumber = ?",user.icon,user.phoneNumber];
    }else
    {
        NSLog(@"没有该用户");
        return;
    }
}
- (void)changeUserName:(NSString *)name phoneNumber:(NSString *)phoneNumber
{
    [_db executeUpdate:@"UPDATE userInfo SET userName= ? WHERE phoneNumber = ?",name,phoneNumber];
}

- (void)changePassword:(NSString *)password phoneNumber:(NSString *)phoneNumber
{
    [_db executeUpdate:@"UPDATE userInfo SET password= ? WHERE phoneNumber = ?",password,phoneNumber];
}
/**
 *  添加用户注册数据到数据库
 */
- (void)addWithUser:(SYUser *)user
{
    if ([self findUserInfophoneNumber:user.phoneNumber]) {
        return;
    }
    NSLog(@"%@",user.phoneNumber);
    [_db executeUpdate:@"insert into userInfo(userName , phoneNumber , password ,icon) values (?,?,?,?)",user.username,user.phoneNumber,user.password,user.icon];
}
/**
 *  取出数据 用户名
 */
- (SYUser *)getUserInfophoneNumber:(NSString *)phoneNumber
{
    SYUser *user = [[SYUser alloc]init];
    FMResultSet *rs = [_db executeQuery:@"SELECT phoneNumber,password,userName,icon FROM userInfo where phoneNumber = ?",phoneNumber];
    while([rs next]) {
       NSString *name = [rs stringForColumn:@"userName"];
       NSString *phoneNumber =[rs stringForColumn:@"phoneNumber"];
        NSString *password =[rs stringForColumn:@"password"];
        NSData *icon =[rs dataForColumn:@"icon"];
        user.icon = icon;
        user.username =name;
        user.phoneNumber =phoneNumber;
        user.password = password;
        
    }[rs close];
    return user;
}
//避免重复注册 注册前遍历数据库
- (BOOL)findUserInfophoneNumber:(NSString *)phoneNumber
{
    SYUser *user = [self getUserInfophoneNumber:phoneNumber];
    if (!user.phoneNumber) {
        return NO;
    }else
    {
       return YES;
    }
}

- (BOOL) isExistTable:(NSString *)tableName
{
    FMResultSet *rs = [_db executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", tableName];
    while ([rs next])
    {
        // just print out what we've got in a number of formats.
        NSInteger count = [rs intForColumn:@"count"];
        if (0 == count)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    return NO;
}
@end
